// pages/register/register.js
var app = getApp();
var url = app.globalData.url;
var headers;
var waitforcode;
var md = require('../../utils/md5.js');
var Trim = require('../../utils/util.js');
var rxPhone = /^1[3|4|5|7|8][0-9]{9}$/;
var origin=null;
var thisflag=true;//防止重复提交
Page({
  data:{
    url:"../../img",
    info:{
      tel:"",
      icode:"",
      pwd:"",
      rpwd:"",
      isok:false
    },
    i: 60,
    waitforcode: false,
  },
  waitForCode: function () {
    var that = this
    waitforcode = true;
    this.setData({
      waitforcode: waitforcode
    })
    if (waitforcode) {
      var i = 60;
      var timer = setInterval(function () {
        
        i--
        that.setData({
          i: i
        })
        if (i < 0) {
          waitforcode = false;
          that.setData({
            waitforcode: waitforcode,
          })
          clearInterval(timer)
        }
      }, 1000)
    }
  },
  getcode:function(){
    var that = this;
    var phone=this.data.info.tel;
    if(!phone){
      wx.showModal({
        title: '来自伴行的提示',
        content: '请输入手机号',
        showCancel:false
      })
      return;
    } else if (!rxPhone.test(phone)){
      wx.showModal({
        title: '来自伴行的提示',
        content: '手机号码有误',
        showCancel: false
      })
      return;
    }
    if (!waitforcode) {
      wx.request({
        url: url + 'api/get_sms_verify_code',
        data: {
          "phone_num": "" +Trim.trim(that.data.info.tel)+ "",
          "types": "reset"
        },
        success: function (data) {
          console.log(Trim.trim(that.data.info.tel))
          if (data.data.result === 1) {
            that.waitForCode();
            wx.showModal({
              title: '来自伴行的提示',
              content: "验证码已发送，请注意查收",
              showCancel: false,
            });            
          } else {
            wx.showModal({
              title: '来自伴行的提示',
              content: data.data.message,
              showCancel: false,
            })
          }
        }
      })
    }
  },
  showMode:function(mes){
    wx.showModal({
      title: '来自伴行的提示',
      content: mes,
      showCancel: false,
    });
  },
  changePsw:function(){
    var info=this.data.info;
    if (!Trim.trim(info.tel)){
      this.showMode("手机号不可为空")
      return;   
    } else if (!Trim.trim(info.pwd)){
      this.showMode("密码不可为空")
      return;
    } else if (Trim.trim(info.pwd) != Trim.trim(info.rpwd)){
      this.showMode("两次密码不一致")
      return;
    } else if (!thisflag){
      this.showMode("表单已提交请耐心等待")
      return;
    }
    if (info.isok) {
      thisflag=false
      wx.request({
        url: url + 'api/reset_pwd_by_mobile',
        header:headers,
        method: "POST",
        data: {
          "phone_num": Trim.trim(info.tel),
          "verification_code": Trim.trim(info.icode),
          "password": md.md5(md.md5(Trim.trim(info.pwd)))
        },
        success: function (data) {
          thisflag=true;
          console.log(info)
          console.log(data)
         wx.showModal({
           title: '来自伴行的提示',
           content: data.data.message,
           showCancel:false,
           success:function(res){
             if (res.confirm &&data.data.result===1){//点击确定
               if (origin){
                 wx.redirectTo({
                   url: '../toLogin/toLogin',
                 })
               }else{
                 wx.switchTab({
                   url: '../../pages/my/my',
                 })
               }
             }
           }
         })
        }
      })
    }
  },
  ident:function(e){
    console.log('blur')
    var kdata = this.data.info;    
    kdata[e.target.dataset.i]=e.detail.value;
    console.log(kdata) ;
    var ok =Boolean(kdata.tel&&kdata.icode&&kdata.pwd&&kdata.rpwd); 
    if(ok){
      kdata.isok=true;
    }else{
      kdata.isok=false;
    }
    this.setData({
      info:kdata
    })
    console.log(this.data.info)
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    if (options.from){
      origin =true
    }
    

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    headers = app.globalData.loginheader;
    console.log(getCurrentPages())
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})